<?php

namespace App\Model;

use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

/**
 * Class Comment
 *
 * @package App\Model
 * @property-read \App\User $user
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Comment onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Comment withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Comment withoutTrashed()
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment query()
 * @property int $id
 * @property int $a_id 文章id
 * @property int $user_id 用户id
 * @property string $content 评论内容
 * @property int $is_stick 置顶0不置顶1置顶
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @property string|null $deleted_at
 * @property int $thumb 点赞
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment whereAId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment whereIsStick($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment whereThumb($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Comment whereUserId($value)
 */
class Comment extends Model
{
    use SoftDeletes;
    protected $table = 'comments';

    protected $hidden = ['deleted_at'];

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function listByArticle($id)
    {
        $comments = $this->select('comments.id','comments.user_id','comments.thumb', 'comments.content', 'comments.created_at', 'user_detail.wechat_name', 'user_detail.head_portrait', 'cards.user_name', 'cards.head_picture','vip_fee_config.type as vip_level', 'vip_fee_config.icon_path as vip_level_icon')
            ->leftJoin('cards', 'comments.user_id', '=', 'cards.user_id')
            ->leftJoin('user_detail', 'comments.user_id', '=', 'user_detail.user_id')
            ->leftJoin('user_grade', 'comments.user_id', '=', 'user_grade.user_id')
            ->leftJoin('vip_fee_config', 'user_grade.vip_fee_config_id', '=', 'vip_fee_config.id')
            ->where('comments.a_id', '=', $id)
            ->orderBy('created_at', 'desc')
            ->paginate(request('limit', 5));
        $data = $comments->toArray();
        if (Auth::check()) {
            $ids = collect($comments->items())->pluck('id');
            $v = Vote::where('type', 2)->where('user_id', Auth::id())->whereIn('target_id', $ids)->pluck('target_id');
        }
        $data['data'] = [];
        foreach ($comments as $comment) {
            $tmp = [
                'id' => $comment->id,
                'user' => [
                    'id' => $comment->user_id,
                    'name' => $comment->user_name?:$comment->wechat_name,
                    'portrait' => $comment->head_picture?:$comment->head_portrait,
                    'level' => $comment->vip_level?:0,
                    'level_icon' => $comment->level_icon?:''
                ],
                'content' => $comment->content,
                'thumb' => $comment->thumb,
                'created_at' => $comment->created_at->format('Y-m-d H:i:s'),
                'is_thumb' => false
            ];
            if (Auth::check()){
                if (in_array($comment->id, $v->toArray())){
                    $tmp['is_thumb'] = true;
                }
            }
            $data['data'][] = $tmp;
        }
        return $data;
    }

    public static function getComments($article_id)
    {
        return \DB::table('comments')->where('a_id', $article_id)->get();
    }
}
